<template>
  <ele-modal
    form
    :width="1000"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        row-key="id"
        row-click-checked
      >
        <template #toolbar="{}">
          <el-button type="primary" @click="payDone()"> 下一步 </el-button>
        </template>

        <template #price="{ row }">
          <span class="pointer blue" @click="openDetail(row)">{{
            row.price
          }}</span>
        </template>

        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>

        <template #pay_qrcode="{ row }">
          <span
            v-if="row.pay_qrcode !== ''"
            class="pointer blue"
            @click="openQrImage(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>

        <template #action="{ row }">
          <el-button
            :icon="Upload"
            type="primary"
            size="small"
            @click="openPayImages(row)"
          >
            付款图片
          </el-button>
          <span> （{{ row.payImagesCount }}） </span>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 报价 -->
    <offer-b-detail v-model="showDetail" :offer-id="offerId" />
    <!-- 保险报价图片 -->
    <offer-images v-model="showImages" :data="current" />
    <!-- 保险报价图片 -->
    <qr-images v-model="showQrImages" :data="current" />
    <!-- 保险付款图片 -->
    <offer-pay-images
      v-model="showPayImages"
      :data="current"
      @done="uploadDone"
    />
  </ele-modal>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessage, ElMessageBox } from 'element-plus/es';
  import { pageInsuranceOffer, setTradeToPayDone } from '@/api/company';
  const emit = defineEmits(['done', 'update:modelValue']);
  import OfferBDetail from '@/views/insurance/components/offer-b-detail.vue';
  import OfferImages from './offer-images.vue';
  import { Upload } from '@element-plus/icons-vue';
  import OfferPayImages from './offer-pay-images.vue';
  import QrImages from './qr-images.vue';
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    data: Object
  });
  const title = ref('');
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    if (props.action === 'choose') {
      cols.push({
        type: 'selection',
        columnKey: 'selection',
        width: 50,
        align: 'center',
        fixed: 'left'
      });
    }

    cols.push(
      {
        width: 120,
        prop: 'cate',
        label: '类型'
      },
      {
        prop: 'insurance',
        label: '保险公司'
      },
      {
        width: 120,
        prop: 'price',
        label: '价格'
      },
      {
        width: 100,
        prop: 'rebate',
        label: '返点'
      },
      {
        width: 100,
        slot: 'img',
        label: '报价图片',
        align: 'center'
      },
      {
        width: 100,
        slot: 'pay_qrcode',
        label: '付款二维码',
        align: 'center'
      },
      {
        width: 160,
        slot: 'action',
        label: '操作'
      }
    );

    return cols;
  });

  const current = ref([]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 表格数据源 */
  var offerList = ref({});
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsuranceOffer({
      ...where,
      ...orders,
      page,
      limit,
      choose: 1,
      cash: 1,
      trade_id: props.data.id
    });
    offerList.value = result.list;
    return result;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 报价详情 */
  const offerId = ref(0);
  const showDetail = ref(false);
  const openDetail = (row) => {
    if (row.cate !== '商业险') {
      ElMessage.warning('不是商业险无报价详情');
      return false;
    }
    offerId.value = row.id;
    showDetail.value = true;
  };

  /** 查看报价付款图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 查看付款二维码图片 */
  const showQrImages = ref(false);
  const openQrImage = (data) => {
    current.value = data;
    showQrImages.value = true;
  };

  /** 付款图片上传完成 */
  const uploadDone = () => {
    reload();
  };

  // 提交完成
  const payDone = () => {
    ElMessageBox.confirm(
      props.data.plate_number + '的保险已全部付完款？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        var requestData = {
          id: props.data.id,
          plate_number: props.data.plate_number,
          list: JSON.stringify(offerList.value)
        };
        setTradeToPayDone(requestData)
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
            emit('done');
          })
          .catch((e) => {
            loading.close();
            console.log(e);
          });
      })
      .catch(() => {});
  };

  /** 付款图片 */
  const showPayImages = ref(false);
  const openPayImages = (data) => {
    current.value = data;
    showPayImages.value = true;
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = '待付款列表 - ' + props.data.plate_number;
      } else {
        console.log('关闭');
      }
    }
  );
</script>
